package com.cas.client;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jasig.cas.client.authentication.AttributePrincipal;

@WebServlet(name="HelloWorldExample",urlPatterns="/HelloWorldExample")
public class HelloWorldExample extends HttpServlet {  
    private static final long serialVersionUID = 1L;  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws IOException, ServletException {  
        response.setContentType("text/html");  
        response.setCharacterEncoding("UTF-8");  
        PrintWriter out = response.getWriter();  
  
        out.println("<html>");  
        out.println("<head>");  
  
        String title = "Hello";  
  
        out.println("<title>" + title + "</title>");  
        out.println("</head>");  
        out.println("<body bgcolor=\"white\">");  
  
  
        out.println("<a href=\"../helloworld.html\">");  
        out.println("<img src=\"../images/code.gif\" height=24 "  
                + "width=24 align=right border=0 alt=\"view code\"></a>");  
        out.println("<a href=\"../index.html\">");  
        out.println("<img src=\"../images/return.gif\" height=24 "  
                + "width=24 align=right border=0 alt=\"return\"></a>");  
        out.println("<h1>" + title + "</h1>");  
        //以下是两种获取用户信息的两种方式，分别与Web.XML中的配置相对应，大家结合理解  
          
        // 通过 CAS HttpServletRequest Wrapper Filter 获取用户信息  
        String userNameString = request.getRemoteUser();  
          
        AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();  
          
        if (null != principal) {  
            Map<String, Object> attMap = principal.getAttributes();  
            out.println("返回参数总个数  = " + attMap.size() + "<br>");  
            for (Entry<String, Object> entry : attMap.entrySet()) {  
                out.println("     | " + entry.getKey() + "=:" + entry.getValue() + "<br>");  
            }         
            String username = null;  
            out.print("用户名:");  
            if (null != principal) {  
                username = principal.getName();  
                out.println("<span style='color:red;'>" + username + "</span><br>");  
            }  
        }  
          
          
          
        // 通过CAS Assertion Thread Local Filter  获取用户信息，共两种方式  
//      Assertion assertion = (Assertion) request.getSession().getAttribute(  
//              AbstractCasFilter.CONST_CAS_ASSERTION);  
//      if (null != assertion) {  
//            
//          Map<String, Object> attMap = assertion.getPrincipal().getAttributes();  
//          out.println(" Log | getAttributes Map size = " + attMap.size() + "<br>");  
//          for (Entry<String, Object> entry : attMap.entrySet()) {  
//              out.println("     | " + entry.getKey() + "=:" + entry.getValue() + "<br>");  
//          }  
//            
//          AttributePrincipal principal = assertion.getPrincipal();  
//          // AttributePrincipal principal = (AttributePrincipal) request  
//          // .getUserPrincipal();  
//          String username = null;  
//          out.print(" Log | UserName:");  
//          if (null != principal) {  
//              username = principal.getName();  
//              out.println("<span style='color:red;'>" + username + "</span><br>");  
//          }  
//      }  
  
        out.println("</body>");  
        out.println("</html>");  
    }  
}  